package com.liser.southernstar.utils;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * 生成RSA256密钥对
 */
public class RSA256KeyGenerator {
    private static final String KEY_ALGORITHM = "RSA";
    private static final int KEY_SIZE = 2048;

    /**生成RSA公私钥*/
    public static RSAKey gen() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
        keyPairGenerator.initialize(KEY_SIZE, new SecureRandom());
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        return new RSAKey(keyPair.getPublic().getEncoded(), keyPair.getPrivate().getEncoded());
    }

    /**存储生成结果*/
    public static class RSAKey{
        private byte[] publicKey;
        private byte[] privateKey;
        public RSAKey(byte[] publicKey, byte[] privateKey) {
            this.publicKey = publicKey;
            this.privateKey = privateKey;
        }
        public byte[] getPublicKey() {
            return publicKey;
        }
        public byte[] getPrivateKey() {
            return privateKey;
        }
    }
}
